package com.ruoyi.controller.controller.customer;


import com.ruoyi.common.core.controller.CustomerBaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.admin.AdminCustomerDB;
import com.ruoyi.common.core.domain.entity.customer.CustomerIntentionDB;
import com.ruoyi.system.service.admin.AdminCustomerService;
import com.ruoyi.system.service.customer.IntentionServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

// 白名单：SecurityConfig.java
@RestController
@RequestMapping("/customer/intention")
@Slf4j
public class IntentionController extends CustomerBaseController {

    // 顾客意向
    @Resource
    private IntentionServiceImpl intentionService;
    @Resource
    private AdminCustomerService adminCustomerService;

    @PostMapping("add")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody CustomerIntentionDB req, @RequestHeader(value = "tenant-id", required = false) Long tenantId) {

        intentionService.add(req);

        // 查询用户是否存在
        AdminCustomerDB userByPhone = adminCustomerService.getUserByPhone(req.getUserPhoneNumber());
        if (ObjectUtils.isNotEmpty(userByPhone)){
            return toAjax(true);
        }
        AdminCustomerDB adminCustomerDB = new AdminCustomerDB();
        adminCustomerDB.setUserName(req.getUserName());
        adminCustomerDB.setGender(req.getGender());
        adminCustomerDB.setUserPhoneNumber(req.getUserPhoneNumber());
        // 前端传租户id=2（耘享农场）
        log.info("请求头tenantId:{}", tenantId);
        adminCustomerDB.setTenantId(tenantId);
        adminCustomerService.insertAdminCustomer(adminCustomerDB);
        return toAjax(true);
    }

    @GetMapping("find")
    public AjaxResult find(@RequestParam("userPhoneNumber") String userPhoneNumber) {
        return success(intentionService.find(userPhoneNumber));
    }

    @DeleteMapping("delete/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        intentionService.delete(ids);
        return success();
    }

    @GetMapping("findRemaining")
    public AjaxResult findRemaining() {
        // findRemaining
        List<Map<String, Object>> result = intentionService.findRemaining();
        return success(result);
    }

//    /**
//     * 查询系统租户列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:tenant:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(SysTenantDB sysTenant)
//    {
//        startPage();
//        List<SysTenantDB> list = sysTenantServiceImpl.selectSysTenantList(sysTenant);
//        return getDataTable(list);
//    }

//    /**
//     * 导出系统租户列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:tenant:export')")
//    @Log(title = "系统租户", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysTenantDB sysTenant)
//    {
//        List<SysTenantDB> list = sysTenantServiceImpl.selectSysTenantList(sysTenant);
//        ExcelUtil<SysTenantDB> util = new ExcelUtil<SysTenantDB>(SysTenantDB.class);
//        util.exportExcel(response, list, "系统租户数据");
//    }

//    /**
//     * 获取系统租户详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:tenant:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(sysTenantServiceImpl.selectSysTenantById(id));
//    }

//    /**
//     * 新增意向
//     */
//    @PreAuthorize("@ss.hasPermi('system:tenant:add')")
//    @Log(title = "系统租户", businessType = BusinessType.INSERT)
//    @PostMapping
//    @Transactional(rollbackFor = Exception.class)
//    public AjaxResult add(@RequestBody CustomerIntentionDB req)
//    {
//
//        intentionService.add(req);
//        // 查询该名称的租户
//        sysTenantServiceImpl.insertSysTenant(sysTenant);
//        SysTenantDB tenantByPhoneAndName = sysTenantServiceImpl.getTenantByPhoneAndName(sysTenant.getPrimaryPhone(), sysTenant.getTenantName());
//        initTenantServiceImpl.init(tenantByPhoneAndName);
//        return toAjax(true);
//    }
//
//    /**
//     * 修改系统租户
//     */
//    @PreAuthorize("@ss.hasPermi('system:tenant:edit')")
//    @Log(title = "系统租户", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody SysTenantDB sysTenant)
//    {
//        return toAjax(sysTenantServiceImpl.updateSysTenant(sysTenant));
//    }
//
//    /**
//     * 删除系统租户
//     */
//    @PreAuthorize("@ss.hasPermi('system:tenant:remove')")
//    @Log(title = "系统租户", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(sysTenantServiceImpl.deleteSysTenantByIds(ids));
//    }


}
